/**
 * 
 */
package com.kelaite.procurement.restful;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ch.qos.logback.classic.Logger;

import com.kelaite.procurement.dto.UserDTO;
import com.kelaite.procurement.entity.TPipelineHeader;
import com.kelaite.procurement.entity.TPipelinePublic;
import com.kelaite.procurement.entity.TUser;
import com.kelaite.procurement.entity.TVendor;
import com.kelaite.procurement.service.PipelineHeaderService;
import com.kelaite.procurement.service.PipelineItemService;
import com.kelaite.procurement.service.PipelinePublicService;
import com.kelaite.procurement.service.UserService;
import com.kelaite.procurement.service.VendorService;
import com.kelaite.procurement.utils.ConvertUtil;
import com.kelaite.procurement.utils.CurrentUserUtils;

@Controller
@RequestMapping("/api")
public class PipelineRestController {
	
	private static final Logger logger = (Logger) LoggerFactory.getLogger(ConvertUtil.class);
	
	@Autowired
	private PipelineHeaderService pipelineHeaderService;

	@Autowired
	private PipelineItemService pipelineItemService;

	@Autowired
	private PipelinePublicService pipelinePublicService;

	@Autowired
	private VendorService vendorService;

	@Autowired
	private UserService userService;

	@ResponseBody
	@RequestMapping(value = "/pipeline", method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
	public int participatePipeline(HttpServletRequest request) {
		String pipelineId = request.getParameter("pipelineId");
		UserDTO userDTO = CurrentUserUtils.getInstance().getUser();
		TUser currentUser = this.userService.getObject(userDTO.getId());
		
		TPipelineHeader pipelineHeader = this.pipelineItemService.getObject(Long.parseLong(pipelineId)).gettPipelineHeader();
		
		TVendor vendor = this.vendorService.getVendorByClientAndSupplier(pipelineHeader.gettCompany(), currentUser.gettCompany());
		
		boolean flag = this.pipelinePublicService.ifAlreadyPipelined(pipelineHeader, vendor);
		
		if(flag == true){
			return 0;   //已进标过
		}else{
			TPipelinePublic pipelinePublic = new TPipelinePublic();
			pipelinePublic.setCreatedBy(currentUser.getUsername());
			pipelinePublic.setDelete(false);
			pipelinePublic.setFinalWin(false);
			pipelinePublic.setGmtCreateTime(new Date());
			pipelinePublic.setGmtUpdateTime(new Date());
			pipelinePublic.setSuggestedWin(false);
			pipelinePublic.settPipelineHeader(pipelineHeader);
			pipelinePublic.settVendor(vendor);
			pipelinePublic.setUpdatedBy(currentUser.getUsername());
			if(this.pipelinePublicService.save(pipelinePublic) != null){
				logger.debug("process pipeline success!");
				return 1;  //参与竞标
			}
		}
		return 2;
	}
}
